﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;
using KeaoMDS.InfoManage;
using KeaoMDS.AppCode;

namespace KeaoMDS.Weight
{
    public partial class YiWanChengXiuGai : BaseForm
    {
        private string m_ChengZhongLeiXing = string.Empty;
        private string m_HuanSuanGongShi = string.Empty;
        private string m_ChengZhongID = string.Empty;
        private DataRow m_SelectRow = null;
        private string m_XiuGaiQianZhongLiang = string.Empty;
        private string m_quxiangbiaoduan = string.Empty;
        public YiWanChengXiuGai(string ChengZhongID, string ChengZhongLeiXing)
        {
            InitializeComponent();
            m_ChengZhongLeiXing = ChengZhongLeiXing;
            m_ChengZhongID = ChengZhongID;
            BindWuLiao();
            BindCheLiang();
            BindDanWei();
            BindYunShuGongSi();
        }

        private void YiWanChengXiuGai_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(m_ChengZhongID))
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, m_ChengZhongID, "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_chengzhong", createCondition);
                m_SelectRow = Dt.Rows[0];
            }

            if (m_SelectRow != null)
            {

                if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("重量调整"))
                {
                    txt_maozhong.Enabled = false;
                    txt_pizhong.Enabled = false;
                    date_maozhong.Enabled = false;
                    date_pizhong.Enabled = false;
                }


                if (m_ChengZhongLeiXing == "入库物料")
                {
                    this.Text = "入库物料信息修改";
                    btn_shezhiquxiang.Enabled = false;
                }
                else
                {
                    this.Text = "出库物料信息修改";
                }
                txt_chepaihao.Text = string.Format("{0}", m_SelectRow["chepaihao"]);
                txt_maozhong.Text = string.Format("{0}", m_SelectRow["maozhong"]);
                txt_pizhong.Text = string.Format("{0}", m_SelectRow["pizhong"]);
                txt_kouzhong.Text = string.Format("{0}", m_SelectRow["kouzhong"]);
                txt_jingzhong.Text = string.Format("{0}", m_SelectRow["jingzhong"]);
                cmb_wuliao.Text = string.Format("{0}", m_SelectRow["wuliaomingcheng"]);
                cmb_xinghao.Text = string.Format("{0}", m_SelectRow["xinghao"]);
                cmb_danweimingcheng.Text = string.Format("{0}", m_SelectRow["danweimingcheng"]);
                txt_fahuozhongliang.Text = string.Format("{0}", m_SelectRow["fahuozhongliang"]);
                cmb_yunshugongsi.Text = string.Format("{0}", m_SelectRow["yunshugongsi"]);
                date_maozhong.Value = Convert.ToDateTime(m_SelectRow["maozhongshijian"]);
                date_pizhong.Value = Convert.ToDateTime(m_SelectRow["pizhongshijian"]);
                txt_beizhu.Text = string.Format("{0}", m_SelectRow["beizhu"]);
                m_XiuGaiQianZhongLiang = txt_jingzhong.Text;
                m_quxiangbiaoduan = string.Format("{0}", m_SelectRow["quxiangbiaoduan"]);
            }
            else
            {
                if (m_ChengZhongLeiXing == "入库物料")
                {
                    this.Text = "入库补票";
                    btn_shezhiquxiang.Enabled = false;
                }
                else
                {
                    this.Text = "出库补票";
                }

                date_maozhong.Value = DateTime.Now;
                date_pizhong.Value = DateTime.Now;
            }
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_maozhong.Text) || string.IsNullOrEmpty(txt_pizhong.Text) || string.IsNullOrEmpty(txt_kouzhong.Text) || string.IsNullOrEmpty(txt_jingzhong.Text)||string.IsNullOrEmpty(txt_fahuozhongliang.Text))
            {
                KeaoUtils.ShowInfoMessage("重量信息不能为空，请检查重试。");
                return;
            }
            if (!KeaoUtils.isNumber(txt_maozhong.Text) || !KeaoUtils.isNumber(txt_pizhong.Text) || !KeaoUtils.isNumber(txt_kouzhong.Text) || !KeaoUtils.isNumber(txt_jingzhong.Text) || !KeaoUtils.isNumber(txt_fahuozhongliang.Text))
            {
                KeaoUtils.ShowInfoMessage("重量信息不正确，请检查重试。");
                return;
            }
            if (string.IsNullOrEmpty(txt_chepaihao.Text))
            {
                KeaoUtils.ShowInfoMessage("车辆信息不正确，请检查重试。");
                return;
            }
            if (string.IsNullOrEmpty(cmb_danweimingcheng.Text))
            {
                KeaoUtils.ShowInfoMessage("单位信息不正确，请检查重试。");
                return;
            }
            if (string.IsNullOrEmpty(cmb_wuliao.Text))
            {
                KeaoUtils.ShowInfoMessage("物料信息不正确，请检查重试。");
                return;
            }
            decimal maoZhong = Convert.ToDecimal(txt_maozhong.Text);
            decimal piZhong = Convert.ToDecimal(txt_pizhong.Text);
            decimal kouZhong = Convert.ToDecimal(txt_kouzhong.Text);
            decimal jingZhong = Convert.ToDecimal(txt_jingzhong.Text);
            if (maoZhong <= 0m || piZhong <= 0m || jingZhong <= 0m)
            {
                KeaoUtils.ShowInfoMessage("重量不正确，请检查重试。");
                return;
            }

            decimal jiSuanFangLiang = 0m;
            if (!string.IsNullOrEmpty(m_HuanSuanGongShi))
            {
                jiSuanFangLiang = KeaoUtils.Calculator(m_HuanSuanGongShi.Replace("T", txt_jingzhong.Text));
            }

            decimal faHuoZhongLiang = Convert.ToDecimal(txt_fahuozhongliang.Text);

            if (m_SelectRow != null)
            {

                TableRow editRow = new TableRow("tbl_chengzhong", m_ChengZhongID, DBCtl.修改);
                editRow.DataCols.Add(new TableCol() { ColName = "chepaihao", ValueMember = txt_chepaihao.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "wuliaomingcheng", ValueMember = cmb_wuliao.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "xinghao", ValueMember = cmb_xinghao.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "danweimingcheng", ValueMember = cmb_danweimingcheng.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "maozhong", ValueMember = maoZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                editRow.DataCols.Add(new TableCol() { ColName = "pizhong", ValueMember = piZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                editRow.DataCols.Add(new TableCol() { ColName = "kouzhong", ValueMember = kouZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                editRow.DataCols.Add(new TableCol() { ColName = "jingzhong", ValueMember = jingZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                editRow.DataCols.Add(new TableCol() { ColName = "fahuozhongliang", ValueMember = faHuoZhongLiang.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                editRow.DataCols.Add(new TableCol() { ColName = "yunshugongsi", ValueMember = cmb_yunshugongsi.SelectedIndex == 0 ? "" : cmb_yunshugongsi.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "quxiangbiaoduan", ValueMember = m_quxiangbiaoduan, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "beizhu", ValueMember = txt_beizhu.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "caozuojilu", ValueMember = string.Format("{0}修改", KeaoUtils._CurrentUserRow["yonghuxingming"]), ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "jisuanfangliang", ValueMember = jiSuanFangLiang.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                editRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "maozhongshijian", ValueMember = date_maozhong.Value, ValueType = DataValueCommonType.日期时间 });
                editRow.DataCols.Add(new TableCol() { ColName = "pizhongshijian", ValueMember = date_pizhong.Value, ValueType = DataValueCommonType.日期时间 });
                if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                {
                    MainForm.RiZhi(string.Format("修改已完成信息，原始重量【{0}】，修改后重量【{1}】", m_XiuGaiQianZhongLiang, txt_jingzhong.Text));
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    KeaoUtils.ShowInfoMessage("保存失败，请检查重试。");
                    return;
                }
            }
            else
            {
                string chengZhongID = Guid.NewGuid().ToString();
                string chengZhongBianHao = txt_bangdanhao.Text;
                TableRow newRow = new TableRow("tbl_chengzhong", chengZhongID, DBCtl.插入);
                newRow.DataCols.Add(new TableCol() { ColName = "maozhong", ValueMember = maoZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "pizhong", ValueMember = piZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "jingzhong", ValueMember = jingZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "kouzhong", ValueMember = kouZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "pianchazhongliang", ValueMember = "0.00", ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "chepaihao", ValueMember = txt_chepaihao.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "wuliaomingcheng", ValueMember = cmb_wuliao.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "xinghao", ValueMember = cmb_xinghao.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "danweimingcheng", ValueMember = cmb_danweimingcheng.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "quxiangbiaoduan", ValueMember =m_quxiangbiaoduan , ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "chengzhongleixing", ValueMember = m_ChengZhongLeiXing, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "chengzhongbianhao", ValueMember = chengZhongBianHao, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "jisuanfangliang", ValueMember = jiSuanFangLiang.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "maozhongshijian", ValueMember =date_maozhong.Value , ValueType = DataValueCommonType.日期时间 });
                newRow.DataCols.Add(new TableCol() { ColName = "pizhongshijian", ValueMember = date_pizhong.Value, ValueType = DataValueCommonType.日期时间 });
                newRow.DataCols.Add(new TableCol() { ColName = "maozhongrenyuan", ValueMember = KeaoUtils._CurrentUserRow["yonghuxingming"], ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "pizhongrenyuan", ValueMember = KeaoUtils._CurrentUserRow["yonghuxingming"], ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "maozhongtupian1", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "maozhongtupian2", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "maozhongtupian3", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "pizhongtupian1", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "pizhongtupian2", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "pizhongtupian3", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "jiage", ValueMember = "0.00", ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "jin_e", ValueMember = "0.00", ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "beizhu", ValueMember = txt_beizhu.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "caozuojilu", ValueMember =string.Format("{0}补票录入",KeaoUtils._CurrentUserRow["yonghuxingming"]), ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "yunshugongsi", ValueMember = cmb_yunshugongsi.SelectedIndex == 0 ? "" : cmb_yunshugongsi.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "fahuozhongliang", ValueMember = faHuoZhongLiang.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "bangfang", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "磅房"), ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                       
                if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                {
                    if (string.IsNullOrEmpty(txt_bangdanhao.Text) || txt_bangdanhao.Text == "[可以不设置]" || !KeaoUtils.isNumber(txt_bangdanhao.Text))
                    {
                        chengZhongBianHao = KeaoUtils.GetSerialNumber();
                        TableRow updateBianHaoRow = new TableRow() { TableName = "tbl_chengzhong", ID = newRow.ID, Ctl = DBCtl.修改 };
                        updateBianHaoRow.DataCols.Add(new TableCol() { ColName = "chengzhongbianhao", ValueMember =chengZhongBianHao , ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(updateBianHaoRow);
                    }

                    if (KeaoUtils.ShowQuestionMessage("是否打印磅单？") == DialogResult.Yes)
                    {
                        KeaoPrinter.Print(chengZhongID,false);
                    }
                    MainForm.RiZhi(string.Format("补票录入，称重编号【{0}】",chengZhongBianHao));
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    KeaoUtils.ShowInfoMessage("保存称重信息失败，请检查重试。");
                    return;
                }
                    
            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// 重量输入窗口获取焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_maozhong_Click(object sender, EventArgs e)
        {
            TextBox currentTextBox = sender as TextBox;
            currentTextBox.Focus();
            currentTextBox.SelectAll();
        }

        private void txt_maozhong_TextChanged(object sender, EventArgs e)
        {
            if (KeaoUtils.isNumber(txt_maozhong.Text) && KeaoUtils.isNumber(txt_pizhong.Text) && KeaoUtils.isNumber(txt_kouzhong.Text))
            {
                decimal d = decimal.Parse(txt_maozhong.Text) - decimal.Parse(txt_pizhong.Text) - decimal.Parse(txt_kouzhong.Text);
                txt_jingzhong.Text = d.ToString("0.00");
            }
        }

        private void BindCheLiang()
        {
            string createCondition = KeaoUtils._DB.CreateAndCondition("cheliangleixing", DataValueCommonType.字符串, m_ChengZhongLeiXing == "入库物料" ? "入库车辆" : "出库车辆", "", SearchConditionType.等于);
            createCondition += KeaoUtils._DB.CreateAndCondition("cheliangzhuangtai", DataValueCommonType.字符串, "启用", "", SearchConditionType.等于);
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_cheliang", createCondition);
            txt_chepaihao.DisplayMember = "chepaihao";
            txt_chepaihao.ValueMember = "cheliangpizhong";
            txt_chepaihao.DataSource = Dt;
            txt_chepaihao.SelectedIndex = -1;
        }

        private void BindWuLiao()
        {
            try
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, m_ChengZhongLeiXing, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("wuliaozhuangtai", DataValueCommonType.字符串, "启用", "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, m_ChengZhongLeiXing, "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_wuliao", createCondition);
                cmb_wuliao.DisplayMember = "wuliaomingcheng";
                cmb_wuliao.ValueMember = "id";
                cmb_wuliao.DataSource = Dt;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定物料信息异常。{0}", err.Message));
            }
        }

        /// <summary>
        /// 绑定型号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmb_wuliao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable wuLiaoDt = cmb_wuliao.DataSource as DataTable;
                DataRow[] selectRows = wuLiaoDt.Select(string.Format("id='{0}'", cmb_wuliao.SelectedValue));

                m_HuanSuanGongShi = string.Empty;
                string createCondition = KeaoUtils._DB.CreateAndCondition("wuliaobianhao", DataValueCommonType.字符串, string.Format("{0}", cmb_wuliao.SelectedValue), "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_xinghao", createCondition);
                cmb_xinghao.DisplayMember = "xinghao";
                cmb_xinghao.ValueMember = "id";
                cmb_xinghao.DataSource = Dt;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定型号信息异常。{0}", err.Message));
            }
        }

        /// <summary>
        /// 获取换算公式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void cmb_xinghao_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable Dt = cmb_xinghao.DataSource as DataTable;
            DataRow[] selectRow = Dt.Select(string.Format("id='{0}'", cmb_xinghao.SelectedValue));
            m_HuanSuanGongShi = string.Format("{0}", selectRow[0]["huansuangongshi"]);
        }

        private void BindDanWei()
        {
            try
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("danweileixing", DataValueCommonType.字符串, m_ChengZhongLeiXing == "入库物料" ? "入库单位" : "出库单位", "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_danwei", createCondition);
                cmb_danweimingcheng.DisplayMember = "danweimingcheng";
                cmb_danweimingcheng.ValueMember = "id";
                cmb_danweimingcheng.DataSource = Dt;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定单位信息异常。{0}", err.Message));
            }
        }

        private void BindYunShuGongSi()
        {
            string createCondition = KeaoUtils._DB.CreateAndCondition("gongsizhuangtai", DataValueCommonType.字符串, "启用", "", SearchConditionType.等于);
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_yunshugongsi", createCondition);
            cmb_yunshugongsi.DisplayMember = "gongsimingcheng";
            cmb_yunshugongsi.ValueMember = "id";
            DataRow newRow = Dt.NewRow();
            newRow["gongsimingcheng"] = "—请选择—";
            newRow["id"] = "";
            Dt.Rows.InsertAt(newRow, 0);
            cmb_yunshugongsi.DataSource = Dt;
        }

        private void btn_chepaihao_Click(object sender, EventArgs e)
        {
            CheLiangPaiHao cheLiangPaiHao = new CheLiangPaiHao();
            if (cheLiangPaiHao.ShowDialog() == DialogResult.OK)
            {
                txt_chepaihao.Text = cheLiangPaiHao.m_Current;
            }
        }

        private void txt_bangdanhao_Click(object sender, EventArgs e)
        {
            txt_bangdanhao.Focus();
            txt_bangdanhao.SelectAll();
            txt_bangdanhao.ForeColor = Color.Black;
        }

        private SheZhiQuXiang m_SheZhiQuXinag;

        private void btn_shezhiquxiang_Click(object sender, EventArgs e)
        {
            if (m_SheZhiQuXinag == null)
            {
                m_SheZhiQuXinag = new SheZhiQuXiang();
            }
            if (m_SheZhiQuXinag.ShowDialog() == DialogResult.OK)
            {
                m_quxiangbiaoduan = m_SheZhiQuXinag.m_QuXiangBianHao;
            }
        }


    }
}
